<!-- This collection of functions (libary) is licensed under GPL2.
// See LICENSE or www.gnu.org for more details.

// Dialog functions, generic
-->

<?
if (SODIALOG != "") {
define("SODIALOG", "");

include ("sosqlhi.php");
include ("sostring.php");

define ("DB_SERVER", "localhost");
define ("DB_NAME", "villu");
define ("DB_USER", "");
define ("DB_PASSWD", "");

// Set basic setting for the used database
function initDzBasicSQL() {
    $baseSql = new soBaSQL;
	$baseSql->Server = DB_SERVER;
	$baseSql->Db = DB_NAME;
	$baseSql->User = DB_USER;
	$baseSql->Password = DB_PASSWD;
	
	$baseSql->OrderCols = array();
	$baseSql->WhereCols = array();
	$baseSql->WhereOps = array();
	$baseSql->WhereVals = array();
	$baseSql->DataCols = array();
	$baseSql->DataVals = array();

	return $baseSql;
}


/**
	Show a general pick up dialog. Items to the list is taken from a database.
*/
function soPickDialog($table, $attribs, $show, $returnAttrib, $returnControl, $rows)
{
	$width = 350;
	$height = $rows*20+160;
	$sX = self.pageXOffset;
	$sY = self.pageYOffset;
	$param = "$table;$attribs;$show;$returnAttrib;$returnControl;$rows";
	$param = soSwapStr($param, ' ', '+', 0);
	echo "window.open('sodialog.php?param=$param', '',
		 'height=$height,width=$width');"; // ,screenX=$sX,screenY=$sY
}; // soPickDialog



function getItemsToList($findParams, $useFilter) {
	$sql = initDzBasicSQL();
	$sql->Action = "SELECT";
	$sql->DataCols = array(soExtractVal($findParams, ';', 2));
	$sql->Table = soExtractVal($findParams, ';', 1);
	$orderIndex = soExtractVal($findParams, ';', 3);
	
	$returnAttrib = soExtractVal($findParams, ';', 4) - 1;
	$tmp = soExtractVal($findParams, ';', 3);
	$showAttribCount = soStrCount($tmp, ',') + 1;
	
	for ($j = 1; $j <= $showAttribCount; $j++) {
		$attrib[] = soExtractVal($tmp, ',', $j)-1;
	}
	
	if ($useFilter != "") {
	    $sql->Cols[SOBC_WHERE]->joinArrayAsKeys(
	        array(soExtractVal($sql->DataCols[0], ',', $attrib[0]+1)));
		$sql->WhereVals	= array($useFilter);
		$sql->CustomWhereClause = "\$Col0 like '\$Val0'";
	} else {
		$sql->customWhereClause = "";
	}	
	
	if (!$sql->Run()) die("sql failed: ".$sql->Clause);
	
	for ($i = 0; $i < count($sql->DataVals); $i++) {
		echo "\n<OPTION value=" . $sql->DataVals[$i][$returnAttrib] . ">" . $sql->DataVals[$i][$attrib[0]];
		for ($j = 1; $j < $showAttribCount; $j++) {
			echo " - ". $sql->DataVals[$i][$attrib[$j]];
		};
	}; // for ($i = 0;
}; // function getItemsToList

} // if (SODIALOG != "")

function outputPickForm($param, $findTF) {
echo "
<!--
	Html part starts here. This defines the form for the pick up form.
-->
<HTML>
<HEAD>
	<TITLE></TITLE>
	<META NAME='AUTHOR' CONTENT='Henri Ranki'>
	<META NAME='CREATED' CONTENT='20000803;14275100'>
	<META NAME='CHANGEDBY' CONTENT='Henri Ranki'>
	<META NAME='CHANGED' CONTENT='20000803;14275100'>
</HEAD>

<script language='JavaScript'>
function itemChoosen() {
	var selInd = self.document.searchListForm.pickBox.selectedIndex;
	if (selInd > -1) {
		var ret = self.document.searchListForm.pickBox.options[selInd].value;
		window.opener.document.". soExtractVal($param, ';', 5) .".value = ret;
	}
	close();
}

</script>

<BODY>
    <FORM NAME='searchListForm' ACTION = 'sodialog.php'>
	<INPUT TYPE=HIDDEN NAME='param' VALUE='".$param."'>
	<H4>Valitse listasta </H4>
	<FONT size=-2>	
	<SELECT NAME='pickBox' SIZE=";
	echo soExtractVal($param, ';', 6) . ">";
	echo getItemsToList($param, $findTF);
	echo "
	</OPTION>
	</SELECT>
	</FONT>
    <BR>
    <TABLE COLUMS=2 ALLIGN='centered' WIDTH='90%'>
    <TR><TH>
		<INPUT TYPE=TEXT NAME='findTF'' VALUE='" . $findTF . "'>
    </TH><TH>
		<INPUT TYPE=SUBMIT NAME='findBtn' VALUE='Etsi'>
    </TH></TR>
	</TABLE>
	<INPUT TYPE=BUTTON NAME='PeruutaBtn' VALUE='peruuta' onClick='window.close();'>
	<INPUT TYPE=BUTTON NAME='ValitseBtn' VALUE='Valitse' onClick='itemChoosen();'>
</FORM>
</BODY>
</HTML>
"; } //function output...

if ($param != "")
	outputPickForm($param, $findTF);

?>
